package com.cskaoyan.service;

import com.cskaoyan.bean.StatInfo.*;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 创建日期: 2022/05/06 11:39
 * @author wangyonghong
 */
@Service
public class StatServiceImpl implements StatService {
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Override
    public UserVo queryUser() {
        UserVo userVo = new UserVo();

        userVo.setColumns( getList(new UserEntity()));
        List<UserEntity> listRows=new ArrayList<>();
        List<String> list = removeDuplicates(marketUserMapper.selectByAllAddTime());
        for (String s : list) {
            UserEntity rowsEntity =new UserEntity() ;
            rowsEntity.setDay(s.substring(0,10));
            rowsEntity.setUsers(marketUserMapper.selectEverydayUser("%"+s.substring(0,10)+"%"));
            listRows.add(rowsEntity);
        }
        userVo.setRows(listRows);
        return userVo;
    }

    @Override
    public StatVo queryOrder() {
        StatVo orderVo = new StatVo();
        orderVo.setColumns( getList(new RowsEntity()));
        List<RowsEntity> listRows=new ArrayList<>();
        List<String> list = removeDuplicates(marketOrderGoodsMapper.selectByAllGoodsTime());
        double count=0;
        for (String s : list) {
            RowsEntity rowsEntity = new RowsEntity();
            rowsEntity.setDay(s.substring(0,10));
            List<Double> doubles = marketOrderMapper.selectEverydayOrder("%" + s.substring(0, 10) + "%");
            for (Double aDouble : doubles) {
                count=aDouble+count;
            }
            rowsEntity.setOrders(doubles.size());
            rowsEntity.setAmount((int)count);
            Integer size = marketOrderMapper.selectEverydayOrderUsers("%"+ s.substring(0,10)+"%").size();
            if (size!=0){
                rowsEntity.setPcr((int)count/size);
                rowsEntity.setCustomers(size);
           }
            listRows.add(rowsEntity);
        }
        orderVo.setRows(listRows);
        return orderVo;
    }

    @Override
    public GoodsVo queryGoods() {
        GoodsVo orderVo = new GoodsVo();
        orderVo.setColumns(getList(  new GoodEntity()));
        List<GoodEntity> listRows=new ArrayList<>();
        List<String> list = removeDuplicates(marketOrderGoodsMapper.selectByAllGoodsTime());
        Integer amount=0;
        Integer number=0;
        for (String s : list) {
            GoodEntity rowsEntity = new GoodEntity();
            rowsEntity.setDay(s.substring(0,10));
            List<OrderPo> orderPos = marketOrderGoodsMapper.selectEverydayGoods("%"+s.substring(0,10)+"%");
            for (OrderPo orderPo : orderPos){
                rowsEntity.setAmount(orderPo.getOrders());
                amount =amount+orderPo.getPrice() * orderPo.getNumber();
                number=  number +orderPo.getNumber();
            }
            rowsEntity.setAmount(amount);
            rowsEntity.setProducts(number);
            rowsEntity.setOrders(orderPos.size());
            listRows.add(rowsEntity);
        }
        orderVo.setRows(listRows);
        return orderVo;
    }
    public  List<String> removeDuplicates(List<String> list) {
        List<String> oldList = new ArrayList<String>();
        for (String s : list) {
            oldList.add( s.substring(0,10));
        }

        List<String> newList=new ArrayList<String>();
        for (String element : oldList) {
            if (!newList.contains(element)) {
                newList.add(element);
            }
        }
        return newList;
    }
    private List<String> getList( Object object) {
        List<String> listColumns=new ArrayList<>();
        Field[] declaredFields  = object.getClass().getDeclaredFields();
        for (Field f:declaredFields) {
            listColumns.add(f.getName());
        }
        return listColumns;
    }
}
